import pymysql
import json
from flask import request


class DB:
    def __init__(self):
        self.conn = pymysql.connect(host='localhost', port=3306, user='root', password='123', db='p8qimo',
                                    charset='utf8', cursorclass=pymysql.cursors.DictCursor)
        self.cursor = self.conn.cursor()

    # 添加  修改  删除
    def update(self, sql):
        self.cursor.execute(sql)
        self.conn.commit()

    # 查询单个
    def one(self, sql):
        self.conn.ping(reconnect=True)
        self.cursor.execute(sql)
        res = self.cursor.fetchone()
        return res

    # 查询所有
    def all(self, sql):
        self.conn.ping(reconnect=True)
        self.cursor.execute(sql)
        res = self.cursor.fetchall()
        return res

    # 关闭
    def close(self):
        self.cursor.close()
        self.conn.close()


db = DB()


# 获取post传递的参数
def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data


import uuid
import os


class Mifile:
    def __init__(self):
        self.dir = './static/files/'

    # 重命名
    def rname(self, filename):
        name = os.path.splitext(filename)[1]
        filename = uuid.uuid4().hex + name
        return filename


my = Mifile()


class Config:
    DEBUG = True
    JSON_AS_ASCII = False
